package edu.jptest.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import edu.jptest.bean.Group;
import edu.jptest.bean.Rank;
import edu.jptest.bean.RegUser;
import edu.jptest.bean.User;
import edu.jptest.util.GetDataBaseConnection;

public class UserDAO {

	// 插入用户(注册时的信息插入数据库，只包括用户的部分信息，其它为默认值)
	public boolean insertUser(RegUser user) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "insert into reg_user(nickname,password,email,validate,reg_time) values (?,?,?,?,?)";
			pstm = connection.prepareStatement(sql);
			System.out.println("用户昵称： " + user.getNickname() + "===========");
			pstm.setString(1, user.getNickname());
			pstm.setString(2, user.getPassword());
			pstm.setString(3, user.getEmail());
			pstm.setString(4, user.getValidate());
			pstm.setDate(5, new java.sql.Date(user.getRegTime().getTime()));
			int result = pstm.executeUpdate();
			System.out.println("result: " + result);
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 判断用户激活信息
	public boolean regValidate(String email, String validateCode) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "select reg_id from reg_user where email=? and validate=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, email);
			pstm.setString(2, validateCode);
			ResultSet resultSet = null;
			resultSet = pstm.executeQuery();
			if (resultSet.next()) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 激活用户，注册用户转正式用户
	public boolean activateRegUser(User user) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "insert into user(user_nickname,user_name,user_password,user_type,user_sex,"
					+ "user_ico,user_email,group_id,rank_id,reg_time,user_score) values(?,?,?,?,?,?,?,?,?,?,?)";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, user.getUserNickname());
			pstm.setString(2, user.getUserName());
			pstm.setString(3, user.getUserPassword());
			pstm.setInt(4, user.getUserType());
			pstm.setString(5, user.getUserSex());
			pstm.setString(6, user.getUserIco());
			pstm.setString(7, user.getUserEmail());
			pstm.setInt(8, user.getGroup().getGroupId());
			pstm.setInt(9, user.getRank().getRankId());
			pstm.setDate(10, new java.sql.Date(user.getRegTime().getTime()));
			pstm.setInt(11, user.getUserScore());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 未激活通过用户邮箱查找注册信息
	public RegUser findByEmail(String email) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "select * from reg_user where email=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, email);
			ResultSet resultSet = null;
			resultSet = pstm.executeQuery();
			RegUser user = new RegUser();
			while (resultSet.next()) {
				user.setRegId(resultSet.getInt(1));
				user.setNickname(resultSet.getString(2));
				user.setPassword(resultSet.getString(3));
				user.setEmail(resultSet.getString(4));
				user.setValidate(resultSet.getString(5));
				user.setRegTime(resultSet.getDate(6));
				return user;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return null;
	}

	// 验证用户邮件激活
	public int userValidate(String email, String validateCode) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "select user_id from user where user_email=? and user_password=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, email);
			pstm.setString(2, validateCode);
			ResultSet resultSet = pstm.executeQuery();
			if (resultSet.next()) {
				return resultSet.getInt(1);
			} else {
				return 0;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return 0;
	}

	// 验证用户登录信息
	public int loginValidate(User user) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "select user_id from user where user_nickname=? and user_password=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, user.getUserNickname());
			pstm.setString(2, user.getUserPassword());
			ResultSet resultSet = pstm.executeQuery();
			if (resultSet.next()) {
				return resultSet.getInt(1);
			} else {
				return 0;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return 0;
	}

	// 删除用户
	public boolean deleteUser(int userId) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "delete from user where user_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, userId);
			return pstm.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 修改用户
	public boolean updateUser(User user) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "update user set user_nickname=?,user_name=?,user_password=?,user_type=?,user_sex=?,user_ico=?,user_email=?,group_id=?,user_score=?,rank_id=?,test_count=?,log_count=?,paper_count=?,reg_time=? where user_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, user.getUserNickname());
			pstm.setString(2, user.getUserName());
			pstm.setString(3, user.getUserPassword());
			pstm.setInt(4, user.getUserType());
			pstm.setString(5, user.getUserSex());
			pstm.setString(6, user.getUserIco());
			pstm.setString(7, user.getUserEmail());
			System.out.println("userdao group_id: "+user.getGroup().getGroupId() );
			pstm.setInt(8, user.getGroup().getGroupId());
			pstm.setFloat(9, user.getUserScore());
			pstm.setInt(10, user.getRank().getRankId());
			pstm.setInt(11, user.getTestCount());
			pstm.setInt(12, user.getLogCount());
			pstm.setInt(13, user.getPaperCount());
			pstm.setDate(14, new java.sql.Date(user.getRegTime().getTime()));
			pstm.setInt(15, user.getUserId());
			return pstm.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// // 对用户登录时进行判断,放于服务层，由getAllUser()方法可得到
	// public boolean judgeUser(String userName, String userPassword) {
	// }
	//
	// // 注册时判断，放于服务层，由getAllUser()方法可得到
	// public boolean judgeUserByName(String userName) {
	// }

	// 通过姓名，查询用户id
	public int getUsreIdByUserName(String user_nickName) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		try {
			String sql = "select user_id from user where user_nickname=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, user_nickName);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				return resultSet.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return 0;
	}

	// 通过用户邮箱查找用户ID
	public int getUserIdByEmail(String email) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		try {
			String sql = "select user_id from user where user_email=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, email);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				return resultSet.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return 0;
	}

	// 通过id，查询用户对象
	public User getUserByUserId(int user_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		User user = new User();
		PreparedStatement pstm = null;
		try {
			String sql = "select * from user where user_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, user_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				user.setUserId(resultSet.getInt(1));
				user.setUserNickname(resultSet.getString(2));
				user.setUserName(resultSet.getString(3));
				user.setUserPassword(resultSet.getString(4));
				user.setUserType(resultSet.getInt(5));
				user.setUserSex(resultSet.getString(6));
				user.setUserIco(resultSet.getString(7));
				user.setUserEmail(resultSet.getString(8));
				GroupDAO groupDAO = new GroupDAO();
				Group group = new Group();
				System.out.println("groupId: " + resultSet.getInt(9));
				group = groupDAO.getGroupById(resultSet.getInt(9));
				user.setGroup(group);
				user.setUserScore(resultSet.getInt(10));
				RankDAO rankDAO = new RankDAO();
				Rank rank = new Rank();
				rank = rankDAO.getRankById(resultSet.getInt(11));
				user.setRank(rank);
				user.setTestCount(resultSet.getInt(12));
				user.setLogCount(resultSet.getInt(13));
				user.setPaperCount(resultSet.getInt(14));
				user.setRegTime(resultSet.getDate(15));
				return user;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 获得所有用户id
	public List<Integer> getAllUsersid() {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> usersId = new ArrayList<Integer>();
		try {
			String sql = "select user_id from user";
			pstm = connection.prepareStatement(sql);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				usersId.add(resultSet.getInt(1));
			}
			return usersId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 获得用户积分前5个人id
	public List<Integer> getMaxUserIdScore() {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> usersId = new ArrayList<Integer>();
		try {
			String sql = "select user_id from user order by user_score desc limit 5";
			pstm = connection.prepareStatement(sql);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				usersId.add(resultSet.getInt(1));
			}
			return usersId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 获得某个团体下的所有用户id
	public List<Integer> getUsersIdByGroupId(int group_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> usersId = new ArrayList<Integer>();
		try {
			String sql = "select user_id from user where group_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, group_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				usersId.add(resultSet.getInt(1));
			}
			return usersId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 获得某个排名下的所有用户id
	public List<Integer> getUsersIdByRankId(int rank_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> usersId = new ArrayList<Integer>();
		try {
			String sql = "select user_id from user where rank_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, rank_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				usersId.add(resultSet.getInt(1));
			}
			return usersId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	public static void main(String[] args) {
		UserDAO userDAO = new UserDAO();
		User user = userDAO.getUserByUserId(3);
		System.out.println("用户名：" + user.getUserNickname());
	}

}
